package com.itheima.service;

import com.itheima.mapper.UserMapper;
import com.itheima.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author mewp
 * @date 2022/12/29 09:48
 **/
@Service
public class StatService {
    @Autowired
    private UserMapper userMapper;

    public List<Map> columnCharts() {
        return userMapper.columnCharts();
    }

    public List<Map> lineCharts() {
        return userMapper.lineCharts();
    }

    public List<Map> pieCharts() {
        /*
            [{
                id: "河北省",
                "drilldown": "河北省",
                "name": "河北省",
                "y": 10,
                "data": [{
                    "name": "石家庄",
                    "y": 4
                }, {
                    "name": "唐山",
                    "y": 3
                }, {
                    "name": "保定",
                    "y": 3
                }]
            }]
       */
        List<Map> resultMapList = new ArrayList<>();
        //查询所有员工数据
        List<User> userList = userMapper.selectAll();
        //根据省份分组
        Map<String, List<User>> provinceMap = userList.stream().collect(Collectors.groupingBy(User::getProvince));
        for (String provinceName : provinceMap.keySet()) {
            int provinceSize = 0;
            Map resultMap = new HashMap();
            resultMap.put("id", provinceName);
            resultMap.put("drilldown", provinceName);
            resultMap.put("name", provinceName);
            //再根据城市分组
            Map<String, List<User>> cityMap = provinceMap.get(provinceName).stream().collect(Collectors.groupingBy(User::getCity));
            List<Map> dataMapList = new ArrayList<>();
            for (String cityName : cityMap.keySet()) {
                Map dataMap = new HashMap();
                dataMap.put("name", cityName);
                int citySize = cityMap.get(cityName).size();
                dataMap.put("y", citySize);
                dataMapList.add(dataMap);
                provinceSize += citySize;
            }
            resultMap.put("y", provinceSize);
            resultMap.put("data", dataMapList);
            resultMapList.add(resultMap);
        }
        return resultMapList;
    }

    public Map pieECharts() {
        Map resultMap = new HashMap();
        Example example = new Example(User.class);
        example.setOrderByClause("province,city");
        //查询所有员工数据
        List<User> userList = userMapper.selectByExample(example);

        //根据省份分组
        List<Map> provinceMapList = new ArrayList<>();

        LinkedHashMap<String, List<User>> provinceMap = userList.stream().collect(Collectors.groupingBy(User::getProvince, LinkedHashMap::new, Collectors.toList()));
        for (String provinceName : provinceMap.keySet()) {
            Map map = new HashMap();
            map.put("name", provinceName);
            map.put("value", provinceMap.get(provinceName).size());
            provinceMapList.add(map);
        }

        List<Map> cityMapList = new ArrayList<>();
        LinkedHashMap<String, List<User>> cityMap = userList.stream().collect(Collectors.groupingBy(User::getCity, LinkedHashMap::new, Collectors.toList()));
        for (String cityName : cityMap.keySet()) {
            Map map = new HashMap();
            map.put("name", cityName);
            map.put("value", cityMap.get(cityName).size());
            cityMapList.add(map);
        }

        resultMap.put("province", provinceMapList);
        resultMap.put("city", cityMapList);
        return resultMap;
    }
}
